#include "Cmn.h"
using namespace std;

//-----Amr Shibiny-----beginning-


bool Client::addevent(string begin_hour, string begin_date, string end_hour, string end_date, string event_name)
{
	//precondition(_logged_);
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	int place = begin_hour.find(':');
	begin_hour[place] = ' ';

	place = begin_date.find_first_of('/');
	if(place == string::npos)place = begin_date.find_first_of('\\');
	begin_date[place] = ' ';

	place = begin_date.find_last_of('/');
	begin_date[place] = ' ';
	if(place == string::npos)place = begin_date.find_last_of('\\');
	begin_date[place] = ' ';

	place = end_hour.find(':');
	end_hour[place] = ' ';

	place = end_date.find_first_of('/');
	if(place == string::npos)place = end_date.find_first_of('\\');
	end_date[place] = ' ';

	place = end_date.find_last_of('/');
	if(place == string::npos)place = end_date.find_last_of('\\');
	end_date[place] = ' ';
	string temp_str;temp_str.push_back(AddEvent);
	temp_str+=" "+_username_+" "+event_name+" "+begin_hour+" "+begin_date+" "+end_hour+" "+end_date ;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);

	_Socket_.Receive(buffer,5);
	temp_str=buffer;
	if(temp_str=="DONE")return true;
	else return false;
	//ENSURE(temp_str == "DONE");
}

char* Client:: getEventStart(string event_name)
{
	ENSURE(_logged_);
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	char* message = new char[45];
	string temp_str;
	temp_str.push_back(ShowStart);
	temp_str+=" "+_username_+" "+event_name;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());

	_Socket_.Send(buffer, temp_str.size()+1);
	_Socket_.Receive(message, 49);
	ENSURE(message != "Error sending time"); //postcondition
	return message;
}
char* Client:: getEventEnd(string event_name){
	ENSURE(_logged_);
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);
	char* message = new char[45];
	string temp_str;
	temp_str.push_back(ShowEnd);
	temp_str+=" "+_username_+" "+event_name;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());

	_Socket_.Send(buffer, temp_str.size()+1);
	_Socket_.Receive(message, 47);
	ENSURE(message != "Error retrieving time"); //postcondition
	return message;

}
void Client:: changeEventBeginning(string event_name, string sub_hour, string sub_date){
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	int place = sub_hour.find(':');
	sub_hour[place] = ' ';

	place = sub_date.find_first_of('/');
	if(place == string::npos)place = sub_date.find_first_of('\\');
	sub_date[place] = ' ';

	place = sub_date.find_last_of('/');
	if(place == string::npos)place = sub_date.find_last_of('\\');
	sub_date[place] = ' ';

	string temp_str;temp_str.push_back(ChangeBegin);
	temp_str+=" "+_username_+" "+event_name+" "+sub_hour+" "+sub_date;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);

	_Socket_.Receive(buffer,5);
	temp_str=buffer;
	//ENSURE(temp_str=="DONE");
}
void Client:: changeEventEnd(string event_name, string sub_hour, string sub_date)
{
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	int place = sub_hour.find(':');
	sub_hour[place] = ' ';

	place = sub_date.find_first_of('/');
	if(place == string::npos)place = sub_date.find_first_of('\\');
	sub_date[place] = ' ';

	place = sub_date.find_last_of('/');
	if(place == string::npos)place = sub_date.find_last_of('\\');
	sub_date[place] = ' ';

	string temp_str;temp_str.push_back(ChangeEnd);
	temp_str+=" "+_username_+" "+event_name+" "+sub_hour+" "+sub_date;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);

	_Socket_.Receive(buffer,5);
	temp_str=buffer;
	//ENSURE(temp_str=="DONE");
}
void Client::deleteevent(string event_name)
{
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);
	string temp_str;temp_str.push_back(DeleteEvent);
	temp_str+=" "+_username_+" "+event_name;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);
	_Socket_.Receive(buffer,5);
	temp_str=buffer;
}

void Client::invitefriends(string nameevent)
{
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);
	string invited, temp;
	string max;
	invited = _username_;
	invited +=" "+  nameevent;
	cout<<"\nEnter number of friends to be invited: ";
	cin>>max;
	invited+=" "+max;
	cout<<"\nWrite the names of the friends to be invited seperated by spcaes then enter 0 to end entering friends process : ";
	
	for(int i = 0; i<stoi(max.c_str());i++)
	{
		cin>>temp;
		invited+=" "+temp;
	}

	char* buffer=new char[invited.size()+1];
	buffer[invited.size()]=0;
	memcpy(buffer,invited.c_str(),invited.size());
	_Socket_.Send(buffer, invited.size()+1);

	_Socket_.Receive(buffer,5);
	invited=buffer;
	

}

//-----------Amr Shibiny--------------Ending-



Client::Client(void):_logged_(false),_username_("anymouse"),_password_("none"),_freindName_("none"),_tempPass_("none")
{
}

bool Client:: SignUp(string username_,string password_){
	using namespace serverCode;
	//three lines write them as they are.
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	string temp_str;temp_str.push_back(SIGN_UP);
	temp_str+=" "+username_+" "+password_;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);

	_Socket_.Receive(buffer,5);
	temp_str=buffer;
	if(temp_str=="DONE"){
		return true;
	}
	else return false;
}
bool Client::login(string username_,string password_){
	using namespace serverCode;
	CSocket _Socket_;
	_Socket_.Create();
	_Socket_.Connect(IP,PORT);

	_username_=username_;
	_password_=password_;
	string temp_str;temp_str.push_back(SIGN_IN);
	temp_str+=" "+username_+" "+password_;

	char* buffer=new char[temp_str.size()+1];
	buffer[temp_str.size()]=0;
	memcpy(buffer,temp_str.c_str(),temp_str.size());
	_Socket_.Send(buffer, temp_str.size()+1);

	_Socket_.Receive(buffer,1000);
	temp_str=buffer;
	if(*buffer==WELCOME){
		char* temp;
		strtok_s(buffer, " ", &temp);
		_tempPass_=strtok_s(0, " ", &temp);
		_logged_=true;
		return true;
	}
	else return false;
}
bool Client::signOut(){
	if(_logged_){
		using namespace serverCode;
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP, PORT);
		string temp_str;
		temp_str.push_back(SIGN_OUT);
		temp_str+=" "+_username_+" "+_tempPass_;
		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);


		_Socket_.Receive(buffer,6);
		temp_str=buffer;
		if(temp_str=="DONE"){
			_logged_=false;
			return true;
		}
		return false;
	}
}

int Client::Add_Frind(std::string username_,std::string freindName_ )
{
	using namespace errHandling;
	using namespace serverCode;
	if(_logged_)
	{
	
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP,PORT);

		_username_= username_ ;
		_freindName_ = freindName_ ;
		string temp_str;temp_str.push_back(ADD_FRIEND);
		temp_str+=" "+username_+" "+freindName_ +" "+ADD_FRIEND;

		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);


		_Socket_.Receive(buffer,1000);
		temp_str=buffer;

		if(temp_str=="DONE") return OK ;
			else return NO_ADD_FREIND;
	}
	else return NOT_LOGIN;
}

int Client::Remove_Frind(std::string username_,std::string freindName_ )
{
	using namespace errHandling;
	using namespace serverCode;
	if(_logged_)
	{
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP,PORT);

		_username_= username_ ;
		_freindName_ = freindName_ ;
		string temp_str;temp_str.push_back(REMOVE_FRIEND);
		temp_str+=" "+username_+" "+freindName_ +" "+REMOVE_FRIEND;

		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);
		_Socket_.Receive(buffer,1000);
		temp_str=buffer;

		if(temp_str=="DONE") return OK ;
			else return NO_REMOVE_FREIND;
	}
	else return  NOT_LOGIN;
}
char* Client::ShowFrinds(std::string username_)
{
	char * p;
	using namespace errHandling;
	using namespace serverCode;
	if(_logged_)
	{
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP,PORT);

		_username_= username_ ;
		string temp_str;temp_str.push_back(SHOW_FRIEND);
		temp_str+=" "+username_+" "+SHOW_FRIEND;

		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);
		_Socket_.Receive(buffer,10000);
		return buffer;
		
	}
	
}


char* Client::Showrequests(std::string username_)
{
	char * p;
	using namespace errHandling;
	using namespace serverCode;
	if(_logged_)
	{
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP,PORT);

		_username_= username_ ;
		string temp_str;
		temp_str.push_back(SHOW_REQUEST);
		temp_str+=" "+username_+" "+SHOW_REQUEST;//need to remove the last element

		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);
		_Socket_.Receive(buffer,10000);
		return buffer;
	}
	
}
int Client::Cancel_request(std::string username_,std::string freindName_ )
{
	using namespace errHandling;
	using namespace serverCode;
	if(_logged_)
	{
		CSocket _Socket_;
		_Socket_.Create();
		_Socket_.Connect(IP,PORT);

		_username_= username_ ;
		_freindName_ = freindName_ ;
		string temp_str;temp_str.push_back(CANCEL_REQUEST);
		temp_str+=" "+username_+" "+freindName_ +" "+CANCEL_REQUEST;

		char* buffer=new char[temp_str.size()+1];
		buffer[temp_str.size()]=0;
		memcpy(buffer,temp_str.c_str(),temp_str.size());
		_Socket_.Send(buffer, temp_str.size()+1);
		_Socket_.Receive(buffer,1000);
		temp_str=buffer;

		if(temp_str=="DONE") return OK ;
			else return NO_Cancel_FREIND;
	}
	else return  NOT_LOGIN;
}
Client::~Client(void)
{
}
